const app = getApp();
var sourceType = [ ['camera'], ['album'], ['camera', 'album'] ];
var sizeType = [ ['compressed'], ['original'], ['compressed', 'original'] ];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ComInfo:'',
    ComName:'',
    Photo:[],
    Mode:2,
    EmployTime:'',
    ModeArr:[{name:'可提供上门回收',code:1},{name:'只接受送货上门',code:2}],
    ModeIdx:1,
    upimg:'../../images/wyczupimgbtn.png',
    IsUpdate:1,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  
//UID：登录人的用户ID  ComInfo  EmployTime：入行时间    ComName：公司名称    Photo：图片  Mode：回收方式：1、可提供上门回收 2、只接受送货上门
  addJbxx:function () {
    var that = this;
    var FjLoginLog = wx.getStorageSync('FjLoginLog'); 
    if (this.data.ComName.length < 2) {
      app.tishi('请输入您的公司名称');return false;
    } 
    
    if (this.data.ComInfo.length < 10) {
      app.tishi('请输入10个字以上的公司简介');return false;
    }  
    if (this.data.Photo.length < 2 || this.data.Photo.length > 16) {
      app.tishi('请上传2-16张经营场所照片');return false;
    }
    var sss = this.data.Photo.join('|');    
    var api_input = {
      UID:FjLoginLog.UID,
      ComName:this.data.ComName,
      ComInfo:this.data.ComInfo,
      Mode:this.data.Mode,
      EmployTime:this.data.EmployTime,
      Photo:sss
    }
   
    app.getDaoPostN('api/FJDispose/FJ_Edit_JianjieInfo',api_input,r=>{
      
        if (r.code==200) {
          app.tishi(r.data);
          setTimeout(function () {
            wx.navigateBack()
          },1000)
        }else{
          app.tishi(r.data);
        }
      })   
    

  },
  onLoad: function (options) {
    this.FJ_Get_JianjieInfo();
  },
  bindModeArr:function (e) {
    this.setData({
      ModeIdx:e.detail.value,
      Mode:this.data.ModeArr[e.detail.value].code
    })
  },
  EmployTime:function (e) {
    this.setData({
      EmployTime:e.detail.value
    })
  },
  ComName:function (e) {
    this.setData({
      ComName:e.detail.value
    })
  },
  ComInfo:function (e) {
    this.setData({
      ComInfo:e.detail.value
    })
  },
  FJ_Get_JianjieInfo:function () {
    if (app.checkFjLogin()) {
      var FjLoginLog = wx.getStorageSync('FjLoginLog'); 
      var api_input = {
        UID:FjLoginLog.UID,
      }
      var that = this;
      app.getDaoPostN('api/FJDispose/FJ_Get_JianjieInfo',api_input,r=>{
       
        if (r.data.Info[0].Photo) {
          var photo = r.data.Info[0].Photo.split('|');
        }else{
          var photo = [];
        }
        if (r.code==200) {
          that.setData({
            ComInfo:r.data.Info[0].ComInfo,
            ComName:r.data.Info[0].ComName,
            Mode:r.data.Info[0].Mode,
            EmployTime:r.data.Info[0].EmployTime,
            Photo:photo,
            IsUpdate:r.data.IsUpdate
          })
        }
        
      })   
    }else{
      wx.navigateTo({ url: '../../pages/login/index' }); 
    } 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  uploadImg:function (e) {
    var that = this;
    var idx = e.currentTarget.dataset.idx;
    wx.chooseImage({
      sourceType: sourceType[2],
      sizeType: sizeType[2],
      count: 1,
      success: function (res) {
        const tempFilePaths = res.tempFilePaths[0];
        wx.showLoading({
          title: '图片上传中',
        })
        wx.uploadFile({
          url: app.data.api_url+'api/FJDispose/FJ_Upload_Img?Type=changsuo&strrsa='+app.uploadImgStr(), 
          filePath: tempFilePaths,
          name: 'file',
          success (r){
            var rs = JSON.parse(r.data);
            if (rs.code == 200) {
           
              var ss = that.data.Photo;
              ss.push(rs.data);
              that.setData({
                Photo:ss
              })
              wx.hideLoading();
             
            } 
          },
          fail(r){
            wx.hideLoading();
            app.tishi('图片上传失败,请检查网络');
          }
        })        
      },
      fail(r){
        app.tishi('图片上传失败,请检查网络');
      }      
    })
  },
  imageDel:function (e) {
    var that = this;
    if (this.data.IsUpdate != 1) {
      app.tishi('已提交的信息不能修改');return false;
    }
    wx.showModal({
      title: '提示',
      content: '确定要删除本张图片吗？',
      success: function (sm) {
        if (sm.confirm) {
            var ind = e.currentTarget.dataset.ind;
            var list = that.data.Photo;
            list.splice(ind,1)
            that.setData({
              Photo:list
            })
        } 
      }
    })    
    

  },   

})